package com.tlxch.server.teacher.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tlxch.dto.TStudentdto;
import com.tlxch.dto.TStudto;
import com.tlxch.mapper.SStudentMapper;
import com.tlxch.pojo.SStudent;
import com.tlxch.result.PageResult;
import com.tlxch.server.teacher.TstudentService;
import com.tlxch.utils.MD5Utils;
import com.tlxch.vo.StuGradeMajorvo;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Page;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TstudentServiceImpl implements TstudentService {
    @Autowired
    SStudentMapper sStudentMapper;
    @Autowired
    MD5Utils md5Utils;

    public void addstu(List<TStudentdto> tStudentdtoList) {
        for (TStudentdto tStudentdto : tStudentdtoList) {
            tStudentdto.setSPassword(md5Utils.MD5Lower(tStudentdto.getSPassword()));
        }
        sStudentMapper.addstu(tStudentdtoList);
    }

    @Transactional
    public StuGradeMajorvo getGradeAndSpecialty() {
        List<String> list1 = sStudentMapper.getGrade();
        List<String> list2 = sStudentMapper.getSpecialty();
        StuGradeMajorvo stuGradeMajorvo = new StuGradeMajorvo();
        stuGradeMajorvo.setGrade(list1);
        stuGradeMajorvo.setMajor(list2);
        return stuGradeMajorvo;
    }

    public PageResult pageQuery(TStudto tStudto) {
        PageHelper.startPage(tStudto.getPage(),tStudto.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<SStudent> page = sStudentMapper.pageQuery(tStudto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    public void updateStu(SStudent student) {
        student.setsPassword(md5Utils.MD5Lower(student.getsPassword()));
        sStudentMapper.updateStu(student);
    }
}
